package com.baidu.elinkagescroll.view;

import com.baidu.elinkagescroll.ChildLinkageEvent;
import com.baidu.elinkagescroll.ILinkageScroll;
import com.baidu.elinkagescroll.LinkageScrollHandler;

import ohos.agp.components.AttrSet;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.app.Context;

/**
 * description :
 *
 * @since 2021/7/15
 */
public class LRecyclerView extends ListContainer implements ILinkageScroll {

    /**
     * init LRecyclerView
     *
     * @param context context
     */
    public LRecyclerView(Context context) {
        this(context, null);
    }

    /**
     * init LRecyclerView
     *
     * @param context context
     * @param attrSet attrSet
     */
    public LRecyclerView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * init LRecyclerView
     *
     * @param context context
     * @param attrSet attrSet
     * @param styleName styleName
     */
    public LRecyclerView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        setLongClickable(false);
    }

    @Override
    public void setChildLinkageEvent(ChildLinkageEvent event) {
    }

    @Override
    public LinkageScrollHandler provideScrollHandler() {
        return new LinkageScrollHandler() {
            @Override
            public void flingContent(Component target, int velocityY) {
            }

            @Override
            public void scrollContentToTop() {
                LRecyclerView.this.scrollTo(0);
            }

            @Override
            public void scrollContentToBottom() {
                BaseItemProvider adapter = LRecyclerView.this.getItemProvider();
                if (adapter != null && adapter.getCount() > 0) {
                    LRecyclerView.this.scrollTo(adapter.getCount() - 1);
                }
            }

            @Override
            public void stopContentScroll(Component target) {
            }

            @Override
            public boolean canScrollVertically(int direction) {
                return LRecyclerView.this.canScroll(direction);
            }

            @Override
            public boolean isScrollable() {
                return true;
            }

            @Override
            public int getVerticalScrollExtent() {
                return getScrollValue(Component.AXIS_Y);
            }

            @Override
            public int getVerticalScrollOffset() {
                return getScrollbarFadingDelay();
            }

            @Override
            public int getVerticalScrollRange() {
                return getScrollValue(Component.AXIS_Y);
            }
        };
    }
}
